//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    /// <summary>
    /// Partial class implementation of TridentTypeInfo
    /// </summary>
    public partial class TridentTypeInfo
    {
        #region CommonSearches
        /// <summary>
        /// Common Searches provided.
        /// </summary>
        public sealed class CommonSearches
        {
            /// <summary>
            /// Prevents a default instance of the <see cref="CommonSearches"/> class from being created.
            /// </summary>
            private CommonSearches()
            {
            }

            /// <summary>
            /// Fetches the trident type info.
            /// </summary>
            /// <param name="name">The name.</param>
            /// <param name="connection">The connection.</param>
            /// <returns>TridentTypeInfo object.</returns>
            public static TridentTypeInfo FetchTridentTypeInfo(string name, Connection connection)
            {
                if (null == connection)
                {
                    throw new ArgumentNullException("connection");
                }

                if (string.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException("name");
                }
                
                TridentTypeInfo tridentTypeInfo = null;

                try
                {
                    TridentTypeInfo.ISearch searchQuery = TridentTypeInfo.ISearch.Create();
                    searchQuery.Query = TridentTypeInfo.ISearch.Name(StringField.Condition.Equals, name);
                    List<TridentTypeInfo> tridentTypeInfos = TridentTypeInfo.Search(searchQuery, connection);
                    if (tridentTypeInfos != null && tridentTypeInfos.Count > 0)
                    {
                        tridentTypeInfo = tridentTypeInfos[0];
                    }
                }
                catch (RecordNotFoundException)
                {
                }

                return tridentTypeInfo;
            }
        }
        #endregion
    }
}
